
rm(list=ls())
library(ggExtra)
library(ggplot2)

## load results
load("Eisner/res_eisner.RData")

## load allpathway
load("Eisner/allpathwayuri.RData")


#######################################################################################

sortpathways <- function(rejections, pathways){
  ## 1 sort pathways by their length
  pathways_sort = pathways[order(unlist(lapply(pathways, length)))]
  
  ## ctgt results with the same sort of pathway length
  ctgt = unname(rejections$ctgt)
  resctgt = c()
  for(i in 1:length(ctgt)){
    for(j in 1:length(pathways_sort)){
      if(identical(ctgt[[i]], pathways_sort[[j]])){
        resctgt[i] = j
      }
    }
  }
  
  ctgtda = as.data.frame( cbind(ctgt=2, resctgt) )
  
  
  
  
  ## dag res
  dag = unname(rejections$dag)
  resdag = c()
  for(i in 1:length(dag)){
    for(j in 1:length(pathways_sort)){
      if(identical(dag[[i]], pathways_sort[[j]])){
        resdag[i] = j
      }
    }
  }
  dagda = as.data.frame( cbind(dag=3, resdag) )
  
  
  ## sh res
  sh = unname(rejections$sh)
  if(length(sh)==0){
    shda = as.data.frame( cbind(sh=4, 0) )
    colnames(shda) = c("sh", "ressh")
  } else{
    ressh = c()
    for(i in 1:length(sh)){
      for(j in 1:length(pathways_sort)){
        if(identical(sh[[i]], pathways_sort[[j]])){
          ressh[i] = j
        }
      }
    }
    shda = as.data.frame( cbind(sh=4, ressh) )
    
  }
  
  
  ## fl res
  fl = unname(rejections$fl)
  resfl = c()
  for(i in 1:length(fl)){
    for(j in 1:length(pathways_sort)){
      if(identical(fl[[i]], pathways_sort[[j]])){
        resfl[i] = j
      }
    }
  }
  flda = as.data.frame( cbind(fl=5, resfl) )
  
  
  
  ## homsimes res
  hommel = unname(rejections$ctst)
  reshommel = c()
  for(i in 1:length(hommel)){
    for(j in 1:length(pathways_sort)){
      if(identical(hommel[[i]], pathways_sort[[j]])){
        reshommel[i] = j
      }
    }
  }
  
  hommelda = as.data.frame( cbind(ctst=6, reshommel) )
  
  return(list(ctgt = ctgtda,dag = dagda, sh = shda, fl = flda ,ctst = hommelda))
  
}

# sort23 = sortpathways(res_bordbar,allpathway23)
# sort541 = sortpathways(res_almutawa,allpathway541)
# sort760 = sortpathways(res_taware,allpathway760)
sort_eisner = sortpathways(res_eisner,allpathwayuri)

getplot <- function(sortdata, allpathways){
  sortdata = sort_eisner
  allpathways = allpathwayuri
  df = data.frame(
    size=c(
      sortdata$ctgt$resctgt,
      sortdata$ctst$reshommel,
      sortdata$sh$ressh,
      sortdata$fl$resfl,
      sortdata$dag$resdag ),
    method = c(
      rep("CTGT",nrow(sortdata$ctgt)),
      rep("CTST",nrow(sortdata$ctst)),
      rep("SH",nrow(sortdata$sh)),
      rep("FL",nrow(sortdata$fl)),
      rep("DAG",nrow(sortdata$dag))
    ),
    method2 = c(#rep(1,nrow(actda_s)),
      rep(6,nrow(sortdata$ctgt)),
      rep(5,nrow(sortdata$ctst)),
      rep(4,nrow(sortdata$sh)),
      rep(3,nrow(sortdata$fl)),
      rep(2,nrow(sortdata$dag))
    )
  )
  
  
  p = ggplot(df,aes(x=size, y=method2))+geom_point(shape="|") + xlab("Rank of pathway size") +ylab("Method") + xlim(1,length(allpathways)) +
    scale_y_continuous(labels = c("DAG", "FL", "SH","CTST", "CTGT"), sec.axis = sec_axis(~.,labels =  c("88", "89", "102", "139", "144"),name = "Total number of rejections per method" ) )  +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),#legend.key = element_rect(colour = "transparent", fill = "white"),
          panel.background = element_blank(), axis.line = element_line(colour = "black")) 
  
  p
  #ggExtra::ggMarginal(p, type = "histogram", margins = "x", size = 4, bins=2000)
  
}

jpeg("d_eisner.jpg",width = 16, height = 10, units = 'cm', res = 600)
getplot(sort_eisner,allpathwayuri)
dev.off()

# jpeg("d23.jpg",width = 16, height = 12, units = 'cm', res = 600)
# getplot(sort23,allpathway23)
# dev.off()
# 
# jpeg("d541.jpg",width = 16, height = 12, units = 'cm', res = 600)
# getplot(sort541,allpathway541)
# dev.off()
# 
# jpeg("d760.jpg",width = 16, height = 12, units = 'cm', res = 600)
# getplot(sort760,allpathway760)
# dev.off()



